package com.gs.common.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import io.swagger.annotations.ApiOperation;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @title: 孙杨申
 * @date 2021/12/1 21:21
 */


@Configuration
@EnableSwagger2
@EnableKnife4j
public class SwaggerConfig {
//

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //加了ApiOperation注解的类，生成接口文档
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                //包下的类，生成接口文档
                //.apis(RequestHandlerSelectors.basePackage("io.renren.modules.job.controller"))
                .paths(PathSelectors.any())
                .build()
                .directModelSubstitute(java.util.Date.class, String.class);
    }

    /**
     * demo分组
     *
     * @return
     */
    @Bean
    public Docket createRestApiForDisk() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(
                        // 目标包
                        RequestHandlerSelectors.basePackage("com.gs.disk.controller.admin")
                )
                .paths(PathSelectors.any())
                .build()
                // 分组名称
                .groupName("光速网盘后台")
                .pathMapping("/");
    }

    /**
     * 光速壁纸后台
     *
     * @return
     */
    @Bean
    public Docket createRestApiForWapperAdmin() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(
                        // 目标包
                        RequestHandlerSelectors.basePackage("com.gs.disk.modules.headImgAndWallper.controller.admin")
                )
                .paths(PathSelectors.any())
                .build()
                // 分组名称
                .groupName("光速壁纸后台")
                .pathMapping("/");
    }

    /**
     * 移动端接口
     *
     * @return
     */
    @Bean
    public Docket createRestApiForMobile() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(
                        // 目标包
                        RequestHandlerSelectors.basePackage("com.gs.disk.controller.mobile")
                )
                .paths(PathSelectors.any())
                .build()
                // 分组名称
                .groupName("光速网盘移动端")
                .pathMapping("/");
    }

    /**
     * 移动端接口
     *
     * @return
     */
    @Bean
    public Docket createRestWallperApiForMobile() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(
                        // 目标包
                        RequestHandlerSelectors.basePackage("com.gs.disk.modules.headImgAndWallper.controller.mobile")
                )
                .paths(PathSelectors.any())
                .build()
                // 分组名称
                .groupName("光速壁纸小程序")
                .pathMapping("/");
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("api测试文档")
                .description("微微一笑接口测试文档")
                .contact(new Contact("哈哈哈", "https://github.com", "1241913781@qq.com"))
                .version("v1.1.0")
                .build();
    }

}